package com.beiye.drivertransport.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.beiye.drivertransport.bean.LoginUserBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by ky on 2019/12/3.
 */
//状态栏通知被点击
public class NotificationClickReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //todo 跳转之前要处理的逻辑
       // Log.i("TAG", "userClick:我被点击啦！！！ ");
        Bundle bundle = intent.getBundleExtra("desc");
        int sn = bundle.getInt("sn");
        int noticeSn = bundle.getInt("pic_url");
        int sendObject = bundle.getInt("sendtag");
        String creatime = bundle.getString("creatime");
        if(sendObject==2) {//用户发通知
            RequestParams params = new RequestParams(AppInterfaceConfig.BASE_URL + "sys/noticeUser/" + sn);
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    /*Intent newIntent = new Intent(context, NoticeItemActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    newIntent.putExtra("pic_url", noticeSn);
                    newIntent.putExtra("creatime", creatime);
                    context.startActivity(newIntent);*/
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Toast.makeText(context,"网络连接错误",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }else if(sendObject==1)//机构发通知
        {
            LoginUserBean userInfo = UserManger.getUserInfo();
            LoginUserBean.DataBean data = userInfo.getData();
            String userId = data.getUserId();
            JSONObject js_request = new JSONObject();//服务器需要传参的json对象
            try {
                js_request.put("userId", userId);//根据实际需求添加相应键值对
                js_request.put("sn", sn);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            RequestParams params = new RequestParams(AppInterfaceConfig.BASE_URL + "sys/noticeOrg/modMark");
            params.setAsJsonContent(true);
            params.setBodyContent(js_request.toString());
            x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String result) {
                            /*Intent newIntent = new Intent(context, NoticeItemActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                           newIntent.putExtra("pic_url", noticeSn);
                           newIntent.putExtra("creatime", creatime);
                           context.startActivity(newIntent);*/
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            Toast.makeText(context,"网络连接错误",Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
            });

        }



    }
}

